Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

266
Visualizações
Hiding 3 elements in sequence not working using JavaScript "transitionend" event?

3 boxes that explain the issue

WHAT. I have 3 boxes: blue, red and black and I'm trying to transition them one after another, using JavaScript event listeners, in the following order: black, red and then blue.

HOW: https://codepen.io/gremo/pen/XWVeQVP?editors=1010

Basic HTML structure and nesting (pen for the complete example):

<div id="container">
    <div id="blue"></div>
    <div id="red">
        <div id="black"></div>
    </div>
</div>

JavaScript code is very simple:

const blue = document.getElementById('blue');
const red = document.getElementById('red');
const black = document.getElementById('black');

const hideElement = element => {
  console.log(`Hiding element ${element.getAttribute('id')}`);
  element.classList.add(element.dataset.hideClass);
};

black.addEventListener('transitionend', () => hideElement(red)); // when black transition ends, hide the red
red.addEventListener('transitionend', () => hideElement(blue)); // when red transition ends, hide the blue
hideElement(black); // hide the black (start the chain)

THE PROBLEM: after the black ends, blue and red start at the same time... and this is wrong because blue should start hiding after red completes. In addition, console show that the transitionend event listener for blue is called 2 times.

Any help is much appreciated, I've be struggling with this problem since days.

Console errors

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

It seems that because black is a child of red, the transitionend event got bubbled up, causing red to fire the event twice:

document.addEventListener('DOMContentLoaded', () => {
  const blue = document.getElementById('blue');
  const red = document.getElementById('red');
  const black = document.getElementById('black');

  const hideElement = element => {
    console.log(`Hiding element ${element.getAttribute('id')}`);
    element.classList.add(element.dataset.hideClass);
  };

  black.addEventListener('transitionend', (e) => {
    e.stopPropagation();// <-- added this line
    hideElement(red);
  });
  red.addEventListener('transitionend', () => hideElement(blue));
  hideElement(black);
});

(Edited from your codepen; I can't fork it because I'm too lazy to register)

about 4 years ago · Juan Pablo Isaza Relatório

0

transitionend event will trigger all the listeners once executed. As red and black both register the event listener before the first event triggers both a run. To prevent this, you could either pass a function which will add the event listener inside the hide element function or more preferably pass the event alongside the element and call stopPropagation. stopPropagation is an event function which will prevent the propagation of the event, thus only triggering the first one in this case

e?.stopPropagation()

Below is a modified function which should work in your context

document.addEventListener('DOMContentLoaded', () => {
  const blue = document.getElementById('blue');
  const red = document.getElementById('red');
  const black = document.getElementById('black');

  const hideElement = (element,e) => {
    console.log(`Hiding element ${element.getAttribute('id')}`);
    e?.stopPropagation()
    element.classList.add(element.dataset.hideClass);
  };
  black.addEventListener('transitionend', (e) => hideElement(red,e));
  red.addEventListener('transitionend', (e) => hideElement(blue,e));
  hideElement(black);
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda